package demo.MovieInfo;

import java.util.Scanner;

// 电影操作类
public class MovieService {
    private Movie[] movielist;

    public MovieService( Movie[] movielist ) {
        this.movielist = movielist;
    }

    // 输出电影信息
    public void showAllMovies() {
        System.out.println("全部电影信息如下：");
        for ( int i = 0; i < movielist.length; i++ ) {
            Movie movie = movielist[i];
            System.out.println(movie.getId() + " " + movie.getName() + " " + movie.getPrice() + " " + movie.getActor());
        }
    }

    // 按id查询电影
    public void searchMovieById() {
        System.out.println("请输入要查询的id：");
        Scanner input = new Scanner(System.in);
        int id = input.nextInt();
        // 遍历查询电影对象里的数据
        for ( int i = 0; i < movielist.length; i++ ) {
            // 拿到当前电影对象里的数据
            Movie movie = movielist[i];
            // 判断id是否是需要查询的id，查询到立刻中断循环。
            if ( movie.getId() == id ) {
                System.out.println(movie.getId() + " " + movie.getName() + " " + movie.getPrice() + " " + movie.getActor());
                return;
            }
        }
        System.out.println("没有此id");
    }
}
